import java.util.Iterator;
import java.util.Set;

/* Обертка для массива elementData, чтобы массив можно было сохранять в value у дерева (в т.ч. в refvealue и refdata в RefData) */

public class Array<E> implements Iterable<E> {
    Object[] elementData; // non-private to simplify nested class access

    public Array(int initialCapacity) {
        this.elementData = new Object[initialCapacity];
    }

    public Array(Object[] elementData) {
        this.elementData = elementData;
    }

    public Array(final Set<E> set) {
        this.elementData = new Object[set.size()];
        int i = 0;
        for (E e : set)
            elementData[i++] = e;
    }

    final public int size() {
        return elementData.length;
    }

    @SuppressWarnings("unchecked")
    final public E get(int index) {
        return (E) elementData[index];
    }

    @SuppressWarnings("unchecked")
    public E set(int index, E element) {
        E oldValue = (E) elementData[index];
        elementData[index] = element;
        return oldValue;
    }

    public Iterator<E> iterator() {
        return new Itr();
    }

    private class Itr implements Iterator<E> {
        int cursor = 0; // index of next element to return

        // prevent creating a synthetic constructor
        Itr() {
        }

        public boolean hasNext() {
            return cursor != elementData.length;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            return (E) elementData[cursor++];
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }
    }
}